
.686
.model flat, stdcall


include \masm32\include\windows.inc
;include C:\src\inc-make\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\gdi32.inc
include \masm32\include\comctl32.inc

_SIMLY_MOD_LOADER_	equ 1

include helper.inc
include strlib.inc



includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\comctl32.lib



.data?

comStr		dd ?
filename		db 256 dup(?)
hFile		dd ?
lpMod		dd ?
dummy		dd ?

hHeap		dd ?

szBuf		db 2048 dup(?)

.data
szTmpl		db "mod loader error!",0

.code

MOD_INTERF_MAGIC		equ 187561
MOD_INTERF_MAGIC_LEN		= 3

MOD_INTERF_VERSION		equ 1
MOD_INTERF_VERSION_LEN		= 1

MOD_INTERF_CREATING_OFFSET	= MOD_INTERF_MAGIC_LEN+MOD_INTERF_VERSION_LEN

MOD_INTERF_CR_BEGINING_SIZE	equ 4*1024*1024


;-------------------------------------------------------
_@mod_interf_cr_chunk_begin_stack_el struc
	cnkPtr			dd 0
	lpNext_chunkBeg_stackEl	dd 0
_@mod_interf_cr_chunk_begin_stack_el	ends

;-------------------------------------------------------
;-------------------------------------------------------
_@mod_interf_cr_compute_len macro len
	local _@micclExit
			push ebx
			push ecx
			mov ebx,len
			xor eax,eax
			mov cl,8-2
		@@:
			shr ebx,cl
			test ebx,ebx
			jz short _@micclExit

			mov cl,8
			inc eax
			jmp short @b
	_@micclExit:
			pop ecx
			pop ebx
endm
;-------------------------------------------------------
_@stos_len		macro len,bytes
			push ebx
				push len
			mov ebx,bytes
			and bl,3
				pop eax
			shl eax,2
			or al,bl
			inc bl

		@@:
			stosb
			shr eax,8
			dec bl
			jnz short @b
			
			pop ebx
endm
;-------------------------------------------------------
_@mod_interf_cr_chunk_begin macro cnkBegPtr
			push cnkBegPtr
		_@getMem sizeof _@mod_interf_cr_chunk_begin_stack_el
		assume eax: ptr _@mod_interf_cr_chunk_begin_stack_el
			pop [eax].cnkPtr
			push lplpCrModInterf_chunk_begin
			pop [eax].lpNext_chunkBeg_stackEl
			mov lplpCrModInterf_chunk_begin,eax
endm
;-------------------------------------------------------
; eax=ptrNext
; ecx=shift (bytes)
_@mod_interf_cr_chunk_end proc cnkEndPtr
;correct chunk len
;			push ecx
			push edi

				push cnkEndPtr

			mov eax,lplpCrModInterf_chunk_begin
		assume eax: ptr _@mod_interf_cr_chunk_begin_stack_el
			mov ecx,[eax].lpNext_chunkBeg_stackEl
			mov lplpCrModInterf_chunk_begin,ecx
			mov ecx,[eax].cnkPtr
			mov lpCrModInterf_chunk_begin,ecx
		_@freeMem eax

				pop ecx
			mov edi,ecx
			sub ecx,lpCrModInterf_chunk_begin
		_@mod_interf_cr_compute_len ecx

			push ecx
			push eax

			inc eax
			add edi,eax

			push ecx		;len
			push lpCrModInterf_chunk_begin ;from
			add eax,lpCrModInterf_chunk_begin
			push eax		;to
			call ___@cpyMem

			pop eax
			pop ecx

			push edi
			mov edi,lpCrModInterf_chunk_begin
			xchg eax,ecx
		_@stos_len eax,ecx;ecx,eax
			pop edi

			inc ecx
			mov eax,edi

			pop edi
;			pop ecx
			ret
_@mod_interf_cr_chunk_end	endp
;-------------------------------------------------------
;-------------------------------------------------------
.data

lpCrModInterf			dd 0
CrModInterf_len			dd 0
CrModInterf_len_filled		dd 0
lpCrModInterf_cur_pointer	dd 0

lplpCrModInterf_chunk_begin	dd 0
lpCrModInterf_chunk_begin	dd 0
;lpCrModInterf_chunk_len		dd 0
;-------------------------------------------------------


get_mod_var	equ 0
set_mod_var	equ 0
call_mod_func	equ 0

;include ..\lib\mod_man.inc

;include ..\lib\mod_interf.inc
include ..\lib\mod_loader.inc
;include ..\lib\mod_memb.inc

;include ..\lib\mod_interf.inc
;include ..\lib\mod_loader.inc

include ..\lib\mod_bin_interf_creator.inc


.code
;-------------------------------------------------------
error	proc lpStr

		push 0
		push offset szTmpl
		push lpStr
		push 0
		call MessageBoxEx

		ret
error	endp

;-------------------------------------------------------
main:
			push ebx
			push esi
			push edi

		_@initMem

	;--VVV-- Gets Hi/Lo Mem Addrs
		push esi
	_@getMem sizeof SYSTEM_INFO
		mov esi,eax
		test eax,eax
		jz short @f

		push esi
		call GetSystemInfo

		mov eax,[esi+SYSTEM_INFO.lpMinimumApplicationAddress]
	@@:
		mov lowestAccesibleAddr,eax

		test esi,esi
		jz short @f

		mov eax,[esi+SYSTEM_INFO.lpMaximumApplicationAddress]
	@@:
		mov highestAccesibleAddr,eax

		test esi,esi
		jz short @f

	_@freeMem esi
	@@:
		pop esi
	;--AAA-- Gets Hi/Lo Mem Addrs

			call GetCommandLine
			mov comStr,eax

			mov esi,eax
			mov edi,offset filename

		cl_explore_b:
			mov bl,byte ptr [esi]
			test bl,bl
			jz short cl_explore_e

			cmp bl," "
			jz short cl_explore_e
			
			cmp bl,9	;tab
			jz short cl_explore_e

			cmp bl,","
			jz short cl_explore_e
			
			mov byte ptr [edi],bl
			inc edi
			inc esi
			jmp short cl_explore_b
		cl_explore_e:
			mov byte ptr [edi],0

			push 0		;hTemplateFile
			push 0		;dwFlagsAndAttributes
			push OPEN_EXISTING	;dwCreationDistribution
			push 0		;lpSecurityAttributes
			push FILE_SHARE_READ	;dwShareMode
			push GENERIC_READ	;dwDesiredAccess
			push offset filename	;lpFileName
			call CreateFile

			cmp eax,INVALID_HANDLE_VALUE
			jz short after_initing_mod

			mov hFile,eax

			push 0
			push eax
			call GetFileSize

			test eax,eax
			jz short after_initing_mod

			push eax
		_@getMem eax
			mov lpMod,eax

			pop eax

			push 0
			push offset dummy
			push eax
			push lpMod
			push hFile
			call ReadFile

			push eax

			push hFile
			call CloseHandle

			pop eax
			test eax,eax
			jnz short @f
	this_is_not_mod:
		_@freeMem lpMod
			jmp short after_initing_mod
		@@:

			push lpMod
			call IsThisModule
			test eax,eax
			jz short this_is_not_mod

			push lpMod
			call init_module

;mov eax,lpMod

	after_initing_mod:
			mov edi,offset filename
			inc esi

			mov bl,byte ptr [esi]
			test bl,bl
			jnz  cl_explore_b;short

			call call_bin_mod_begin_funcs

			pop edi
			pop esi
			pop ebx

			push 0
			call ExitProcess

end main


